package com.jifeng.api.token.filter;

import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Administrator
 * @version 1.0
 **/
//@Component
public class TokenAuthenticationFilter extends OncePerRequestFilter {


    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        //从安全上下文中拿 到用户身份对象
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        if (!(authentication instanceof OAuth2Authentication)) {
//            return;
//        }
//        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) authentication;
//        Authentication userAuthentication = oAuth2Authentication.getUserAuthentication();
//        //取出用户身份信息
//        String principal = userAuthentication.getName();
//
//        //取出用户权限
//        List<String> authorities = new ArrayList<>();
//        //从userAuthentication取出权限，放在authorities
//        userAuthentication.getAuthorities().stream().forEach(c -> authorities.add(((GrantedAuthority) c).getAuthority()));
//        OAuth2Request oAuth2Request = oAuth2Authentication.getOAuth2Request();
//        Map<String, String> requestParameters = oAuth2Request.getRequestParameters();
//        Map<String, Object> jsonToken = new HashMap<>(requestParameters);
//        if (userAuthentication != null) {
//            jsonToken.put("principal", principal);
//            jsonToken.put("authorities", authorities);
//        }
//        String json = JSON.toJSONString(jsonToken);
//        //解析出头中的token
////        String token = httpServletRequest.getHeader("json-token");
////        if (token != null) {
////            String json = EncryptUtil.decodeUTF8StringBase64(token);
//        //将token转成json对象
//        JSONObject jsonObject = JSON.parseObject(json);
//        //用户身份信息
//        UserEntity userEntity = new UserEntity();
//        String principal2 = jsonObject.getString("principal");
//        System.out.println("principal->" + principal2);
//        userEntity.setAccount(principal);
////            UserEntity userEntity = JSON.parseObject(jsonObject.getString("principal"), UserEntity.class);
//        //用户权限
//        JSONArray authoritiesArray = jsonObject.getJSONArray("authorities");
//        String[] authorities2 = authoritiesArray.toArray(new String[authoritiesArray.size()]);
//        //将用户信息和权限填充 到用户身份token对象中
//        UsernamePasswordAuthenticationToken authenticationToken
//                = new UsernamePasswordAuthenticationToken(userEntity, null, AuthorityUtils.createAuthorityList(authorities2));
//        authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpServletRequest));
//        //将authenticationToken填充到安全上下文
//        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
//        filterChain.doFilter(httpServletRequest, httpServletResponse);
    }

}
